package com.extraty.question3;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

/**
 * @author Created by Neil Wang
 * @version 1.0.0
 * @date 2021/9/7 2:49 下午
 */
public class Solution1 {
    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) return 0;
        char[] chars = s.toCharArray();
        Integer[] dp = new Integer[chars.length];
        for (int i = 0; i < chars.length; i++) dp[i] = getSize(chars, i);
        return Collections.max(Arrays.asList(dp));
    }

    private int getSize(char[] chars, int i) {
        HashSet<Character> characters = new HashSet<>();
        for (int j = i; j < chars.length; j++) {
            if (characters.contains(chars[j])) {
                return characters.size();
            } else characters.add(chars[j]);
        }
        return characters.size();
    }
}
